#include"message.h"
#include<sstream>
#include<iostream>
#include"timeStamp.h"
//                                            格式化颜色色号                颜色深浅                       分隔符                    需要格式化的字段          
    std::string Message::formatFieldWithColor(COLOR_NUMBER colorNumber , bool dark , const std ::string& delimiter , const std :: string& fieldName ,bool colored = true)//格式化字段
    {
        std::stringstream ss;
        if(colored)
        {
            ss<<"\033["<<dark <<";"<<colorNumber<<"m";
        }
        ss<<fieldName;
        if(colored)//如果前面有颜色就需要把色号置为0，保证后面格式化的颜色不一样（只想让fieldName有颜色）
        {
            ss<<"\033[0m";//将颜色置为0
        }
        ss<<delimiter;
        return ss.str();
    }

    Message::Message(LOG_LEVEL level,std::string fileName,const std::string& functionNmae,int lineNumer)
    {
        int pos=0;
        for(int i=fileName.length()-1;i>=0;--i)
        {
            if(fileName[i]=='/')
            {
                pos=i;
                break;
            }
        }
        fileName=fileName.substr(pos+1,fileName.length()-pos);
        m_coloredHeader= FormatHeader(level,fileName,functionNmae,lineNumer,true);
        m_commonHeader = FormatHeader(level,fileName,functionNmae,lineNumer,false);
    }
    //[时间戳][日志级别][文件名称][函数名称][行号]
    std::string Message::FormatHeader(LOG_LEVEL level,const std::string& fileName,const std::string& functionNmae,int lineNumer,bool colored = true)
    {
        //时间戳
        std::stringstream ss;
        ss<<formatFieldWithColor( DEFAULT_COLOR,0," : ",TimeStamp::Now().ToString(),colored);
//formatFieldWithColor(COLOR_NUMBER colorNumber , bool dark , const std ::string& delimiter , const std :: string& fieldName ,bool colored = true)//格式化字段
        //日志级别
        COLOR_NUMBER logColorNumber = LogLevelColor[level];//日志级别对应的色号
        std::string logLevelType = LogLevelTypeString[level];//日志对应的字符串
        ss<<formatFieldWithColor(logColorNumber,1," : ",logLevelType,colored);

        //文件名称
        COLOR_NUMBER fileColor = logFeildColor[FILE_NAME];
        ss<<formatFieldWithColor(fileColor,1," : ","["+ fileName+ "]",colored);

        //函数名称
        COLOR_NUMBER functionColor = logFeildColor[FUNCTION_NAME];
         ss<<formatFieldWithColor(functionColor,1," : ","["+functionNmae+"]",colored);
        //行号
        COLOR_NUMBER lineColor = logFeildColor[LINE_NAME];
         ss<<formatFieldWithColor(lineColor,1," : ","["+std::to_string(lineNumer)+"]",colored); 

         return ss.str();

    }
    std::string Message::ToString(bool colored)const
    {
        return colored ? m_coloredHeader + m_msg : m_commonHeader + m_msg;
    }

/*
    int main()
    {
        Message msg1(INFO,__FILE__,__FUNCTION__,__LINE__);
        Message msg2(ERROR,__FILE__,__FUNCTION__,__LINE__);
        Message msg3(DEBUG,__FILE__,__FUNCTION__,__LINE__);
        Message msg4(FATAL,__FILE__,__FUNCTION__,__LINE__);
        msg1<<"hello xsy 1";
        msg2<<"hello xsy 2";
        msg3<<"hello xsy 3";
        msg4<<"hello xsy 4";
        std::cout<<msg1.ToString()<<std::endl;
        std::cout<<msg2.ToString()<<std::endl;
        std::cout<<msg3.ToString()<<std::endl;
        std::cout<<msg4.ToString()<<std::endl;
        return 0;
    }
 */